package gang.org.springframework.framework.core.type.classreading;

import gang.org.springframework.framework.io.GangDefaultResourceLoader;
import gang.org.springframework.framework.io.GangResource;
import gang.org.springframework.framework.io.GangResourceLoader;
import gang.org.springframework.framework.util.GangClassUtils;

/**
 * @author gang.chen
 * @description
 * @Date 2021/8/3 13:38
 */
public class GangSimpleMetadataReaderFactory implements GangMetadataReaderFactory{

    private final GangResourceLoader resourceLoader;

    public GangSimpleMetadataReaderFactory() {
        this.resourceLoader = new GangDefaultResourceLoader();
    }

    public GangSimpleMetadataReaderFactory(GangResourceLoader resourceLoader){
        this.resourceLoader = resourceLoader != null ? resourceLoader : new GangDefaultResourceLoader();
    }


    @Override
    public GangMetadataReader getMetadataReader(String className) {

        //业务意义->将name中的“.”字符转为"/"
        String resourcePath = GangResourceLoader.CLASSPATH_URL_PREFIX
                + GangClassUtils.convertClassNameToResourcePath(className)
                + GangClassUtils.CLASS_FILE_SUFFIX;


        GangResource resource = resourceLoader.getResource(resourcePath);

        return getMetadataReader(resource);
    }

    @Override
    public GangMetadataReader getMetadataReader(GangResource resource) {
        return new GangSimpleMetadataReader(resource, this.resourceLoader.getClassLoader());
    }
}
